﻿using System;
using System.ComponentModel;
using System.Data;
using System.Web.Script.Serialization;
using System.Web.Script.Services;
using System.Web.Services;
using Module;
using Module.QuanLyDuoc.HeThongNhapXuat;

namespace HospitalManager.Webservice.QuanLyDuoc.HeThongNhapXuat
{
    /// <summary>
    ///     Summary description for wsPhieuXuatKhoSangKho
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    [ScriptService]
    public class wsPhieuXuatKhoSangKho : WebService
    {
        private readonly PhieuXuatKhoSangKho _phieuXuatKhoSangKho = new PhieuXuatKhoSangKho();
        private readonly PhieuYeuCauXuatVatTu _phieuYeuCauXuatVatTu = new PhieuYeuCauXuatVatTu();

        [WebMethod(EnableSession = true)]
        public string PhieuYeuCauXuatVatTu_ChiTiet_SelectByMaPhieu_MaVatTu(string maphieu, string mavattu)
        {
            var data = _phieuYeuCauXuatVatTu.PhieuYeuCauXuatVatTu_ChiTiet_SelectByMaPhieu(maphieu);
            if (data.Rows.Count <= 0) return null;
            var list =
                data.AsEnumerable()
                    .Where(x => x.Field<string>("MaVatTu") == mavattu)
                    .Select(x => new {MaDonVi = x.Field<string>("MaDonVi"), SoLuongCan = x.Field<Int64>("SoLuong")});
            return (new JavaScriptSerializer {MaxJsonLength = int.MaxValue}).Serialize(list);
        }

        [WebMethod(EnableSession = true)]
        public string PhieuXuatKhoSangKho_Insert(string maphieuyeucau, string ngayxuat, string nguoixuat, string nguoitao)
        {
            var obj = new PhieuXuatKhoSangKho
            {
                MaPhieuYeuCau = maphieuyeucau,
                NgayXuat = Convert.ToDateTime(DateTime.ParseExact(ngayxuat, "dd-MM-yyyy", null).ToString("yyyy-MM-dd")),
                NguoiXuat = nguoixuat,
                NguoiTao = nguoitao
            };
            return _phieuXuatKhoSangKho.PhieuXuatKhoSangKho_Insert(obj) ? obj.MaPhieu : string.Empty;
        }

        [WebMethod(EnableSession = true)]
        public bool PhieuXuatKhoSangKho_Update(string maphieu, string maphieuyeucau, string ngayxuat, string nguoixuat, string nguoitao)
        {
            var obj = new PhieuXuatKhoSangKho
            {
                MaPhieu = maphieu,
                MaPhieuYeuCau = maphieuyeucau,
                NgayXuat = Convert.ToDateTime(DateTime.ParseExact(ngayxuat, "dd-MM-yyyy", null).ToString("yyyy-MM-dd")),
                NguoiXuat = nguoixuat,
                NguoiTao = nguoitao
            };
            return _phieuXuatKhoSangKho.PhieuXuatKhoSangKho_Update(obj);
        }

        [WebMethod(EnableSession = true)]
        public string PhieuXuatKhoSangKho_SelectById(string maphieu)
        {
            return JsonMethods.ToJsonString(_phieuXuatKhoSangKho.PhieuXuatKhoSangKho_SelectById(maphieu));
        }

        [WebMethod(EnableSession = true)]
        public bool PhieuXuatKhoSangKho_ChiTiet_Insert(string maphieu, string makho, string mavattu, int soluong, string ghichu, string nguoitao)
        {
            var obj = new PhieuXuatKhoSangKho
            {
                MaPhieu = maphieu,
                MaKho = makho,
                MaVatTu = mavattu,
                SoLuongYeuCau = soluong,
                GhiChu = ghichu,
                NguoiTao = nguoitao
            };
            return _phieuXuatKhoSangKho.PhieuXuatKhoSangKho_ChiTiet_Insert(obj);
        }

        [WebMethod(EnableSession = true)]
        public bool PhieuXuatKhoSangKho_ChiTiet_Deletee(string maphieu, string mavattu)
        {
            return _phieuXuatKhoSangKho.PhieuXuatKhoSangKho_ChiTiet_Delete(maphieu, mavattu);
        }

        [WebMethod(EnableSession = true)]
        public string PhieuXuatKhoSangKho_ChiTiet_SelectByMaPhieu(string maphieu)
        {
            return JsonMethods.ToJsonString(_phieuXuatKhoSangKho.PhieuXuatKhoSangKho_ChiTiet_SelectByMaPhieu(maphieu));
        }

        [WebMethod(EnableSession = true)]
        public string PhieuXuatKhoSangKho_ChiTiet_SelectByMaPhieu_ChiTiet(string maphieu)
        {
            return JsonMethods.ToJsonString(_phieuXuatKhoSangKho.PhieuXuatKhoSangKho_ChiTiet_SelectByMaPhieu_ChiTiet(maphieu));
        }

        [WebMethod(EnableSession = true)]
        public string PhieuXuatKhoSangKho_ChiTiet_SelectByMaPhieuMaVatTu(string maphieu, string mavattu)
        {
            return JsonMethods.ToJsonString(_phieuXuatKhoSangKho.PhieuXuatKhoSangKho_ChiTiet_SelectByMaPhieuMaVatTu(maphieu, mavattu));
        }
    }
}